package com.bert.task;

import com.bert.controller.LogController;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
//@Configuration
public class LogTask {

    @Resource
    private LogController logController;

    private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private Integer num = 1;

    @Scheduled(cron = "*/1 * * * * ?")
    public void test(){
        logController.test(10000);
    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void log(){
        // 设置MDC字段
        ThreadContext.put("traceId", "traceId677");
        ThreadContext.put("sysName", "yst2");
        ThreadContext.put("modName", "yst2biz");
        ThreadContext.put("branch", "{\"applicationId\":944,\"companyId\":2000}");

        String now = formatter.format(LocalDateTime.now());;
        log.info("Task Log number: {}, content: {}", num, now);
        log.error("Task Log ErrNum: {}, content: {}", num, now);
        num++;
        // 清除MDC字段
        ThreadContext.clearMap();
    }

//    @Scheduled(cron = "*/5 * * * * ?")
    public void log1(){
        for (int i = 0; i < 10000; i++) {
            String now = formatter.format(LocalDateTime.now());
            log.info("Task Log number: {}, content: {}", num++, now);
        }
    }

}
